// pages/view-prescription/view-prescription.js
Page({
	data: {
		patientName: '',
		department: '',
		subDepartment: '',
		campus: '',
		prescriptionList: []
	},

	// 根据科室获取对应的处方记录
	getPrescriptionsByDepartment(department, subDepartment) {
		const prescriptions = {
			'内科': {
				'呼吸内科': [
					{
						id: '001',
						date: '2025-08-01',
						status: '已完成',
						doctor: '张医生',
						department: '内科 - 呼吸内科',
						diagnosis: '上呼吸道感染'
					},
					{
						id: '002',
						date: '2025-07-15',
						status: '待配药',
						doctor: '张医生',
						department: '内科 - 呼吸内科',
						diagnosis: '支气管炎'
					}
				],
				'消化内科': [
					{
						id: '003',
						date: '2025-08-02',
						status: '已完成',
						doctor: '李医生',
						department: '内科 - 消化内科',
						diagnosis: '慢性胃炎'
					},
					{
						id: '004',
						date: '2025-07-20',
						status: '已完成',
						doctor: '李医生',
						department: '内科 - 消化内科',
						diagnosis: '胃食管反流'
					}
				],
				'心血管内科': [
					{
						id: '005',
						date: '2025-08-05',
						status: '待配药',
						doctor: '王医生',
						department: '内科 - 心血管内科',
						diagnosis: '高血压'
					},
					{
						id: '006',
						date: '2025-07-25',
						status: '已完成',
						doctor: '王医生',
						department: '内科 - 心血管内科',
						diagnosis: '心律不齐'
					}
				]
			},
			'外科': {
				'骨科': [
					{
						id: '007',
						date: '2025-08-03',
						status: '已完成',
						doctor: '赵医生',
						department: '外科 - 骨科',
						diagnosis: '腰椎间盘突出'
					},
					{
						id: '008',
						date: '2025-07-18',
						status: '已完成',
						doctor: '赵医生',
						department: '外科 - 骨科',
						diagnosis: '膝关节炎'
					}
				],
				'普通外科': [
					{
						id: '009',
						date: '2025-08-04',
						status: '待配药',
						doctor: '钱医生',
						department: '外科 - 普通外科',
						diagnosis: '甲状腺结节'
					}
				]
			},
			'儿科': {
				'儿科门诊': [
					{
						id: '010',
						date: '2025-08-06',
						status: '已完成',
						doctor: '孙医生',
						department: '儿科 - 儿科门诊',
						diagnosis: '小儿感冒'
					},
					{
						id: '011',
						date: '2025-07-28',
						status: '已完成',
						doctor: '孙医生',
						department: '儿科 - 儿科门诊',
						diagnosis: '小儿咳嗽'
					}
				]
			},
			'皮肤科': {
				'皮肤科门诊': [
					{
						id: '012',
						date: '2025-08-07',
						status: '待配药',
						doctor: '周医生',
						department: '皮肤科 - 皮肤科门诊',
						diagnosis: '湿疹'
					},
					{
						id: '013',
						date: '2025-07-30',
						status: '已完成',
						doctor: '周医生',
						department: '皮肤科 - 皮肤科门诊',
						diagnosis: '荨麻疹'
					}
				]
			}
		}

		// 如果没有指定科室，返回空数组
		if (!department || !subDepartment) {
			return []
		}

		// 获取指定科室的处方记录
		return (prescriptions[department] && prescriptions[department][subDepartment]) || []
	},

	onLoad(options) {
		console.log('查看处方页面加载')
		console.log('页面参数:', options)
		
		const data = {}
		
		if (options.patient) {
			data.patientName = options.patient
		}
		
		if (options.department) {
			data.department = options.department
		}
		
		if (options.subDepartment) {
			data.subDepartment = options.subDepartment
		}
		
		if (options.campus) {
			data.campus = options.campus
		}
		
		// 获取对应科室的处方记录
		if (data.department && data.subDepartment) {
			data.prescriptionList = this.getPrescriptionsByDepartment(data.department, data.subDepartment)
		}
		
		this.setData(data)
	},

	viewPrescriptionDetail(e) {
		const prescriptionId = e.currentTarget.dataset.id
		console.log('查看处方详情:', prescriptionId)
		wx.navigateTo({
			url: '/pages/prescription-detail/prescription-detail?prescriptionId=' + prescriptionId + '&patient=' + this.data.patientName
		})
	},

	refillPrescription(e) {
		const prescriptionId = e.currentTarget.dataset.id
		console.log('在线配药:', prescriptionId)
		
		// 跳转到在线配药页面，传递所有必要参数
		wx.navigateTo({
			url: '/pages/online-refill/online-refill?patient=' + this.data.patientName + 
				'&prescriptionId=' + prescriptionId + 
				'&department=' + this.data.department + 
				'&subDepartment=' + this.data.subDepartment + 
				'&campus=' + this.data.campus
		})
	}
})
